import os
import pygame
import copy


class InitMap:
    def __init__(self, *args):
        self.black = 0, 0, 0
        self.white = 255, 255, 255
        pygame.init()  # 初始化
        os.environ['SDL_VIDEO_WINDOW_POS'] = '%d,%d' % (150, 100)   # 控制窗口出现位置
        self.screen = pygame.display.set_mode((1000, 600))  # 创建窗口
        self.font = pygame.font.Font(r'C:\Windows\Fonts\simkai.ttf', 30)  # 显示中文需要字体，否则可略过
        pygame.display.set_caption("魔塔")
        self.hero = pygame.image.load("../资源/图片/勇士.jpg").convert_alpha()
        self.wall = pygame.image.load("../资源/图片/墙.jpg").convert_alpha()
        self.road = pygame.image.load("../资源/图片/路.jpg").convert_alpha()
        self.upstairs = pygame.image.load("../资源/图片/上楼.jpg").convert_alpha()
        self.downstairs = pygame.image.load("../资源/图片/下楼.jpg").convert_alpha()
        self.pos = [[(375 + 48 * j, 25 + 48 * i) for i in range(11)] for j in range(11)]  # 格子定位列表
        self.ls = sum(copy.deepcopy(self.pos), [])
        self.dic = {}               # 存放各物块通道属性
        self.hero_pos = (0, 0)      # 该属性每个图改写
        self.variable_pos = {}      # 每图改写

    def render_font(self, title, num, y):
        word = self.font.render(title+'   '+str(num), 1, self.white)
        self.screen.blit(word, (150, y))

    def draw(self, obj, x, y):
        self.screen.blit(obj, self.pos[x][y])
        try:
            self.ls.remove(self.pos[x][y])
        except ValueError:
            pass

    def draw_hero(self):
        self.screen.blit(self.hero, self.pos[self.hero_pos[0]][self.hero_pos[1]])

    def variable_brick(self):
        for data in self.variable_pos:
            try:
                self.screen.blit(self.variable_pos[data][0], self.pos[data[0]][data[1]])
                self.dic[data] = self.variable_pos[data][1]
            except KeyError:
                pass
